package top.hidoctor.auscultation.feature.activity;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.gson.reflect.TypeToken;
import com.tang.util.DialogUtil;
import com.tang.util.GsonUtil;
import com.tang.util.SPUtil;

import java.util.ArrayList;
import java.util.List;

import top.hidoctor.auscultation.R;
import top.hidoctor.auscultation.adapter.LocalHistoryAdapter;
import top.hidoctor.auscultation.base.BaseActivity;
import top.hidoctor.auscultation.base.Constant;
import top.hidoctor.auscultation.bean.PatientLocalInfo;
import top.hidoctor.auscultation.utils.MyInternalStorage;

public class LocalHistoryActivity extends BaseActivity {
    private RecyclerView recycler;
    private List<PatientLocalInfo> list = new ArrayList<>();
    private ImageButton ivDelete;
    private LocalHistoryAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_local_history);
        initView();
        initEvent();
    }

    private void initEvent() {
        //获取 本地数组文件
        String content = (String) SPUtil.get(LocalHistoryActivity.this, Constant.LOCAL_INFO, "");
        if (!TextUtils.isEmpty(content)) {
            List<PatientLocalInfo> data = GsonUtil.parseJson(content, new TypeToken<List<PatientLocalInfo>>() {
            });
            list.addAll(data);
            adapter.setNewData(data);
        }

    }

    private void initView() {
        ivDelete = findViewById(R.id.iv_delete);
        recycler = findViewById(R.id.recycler);
        Toolbar toolbar = findViewById(R.id.toolbar);
        TextView tvTitle = findViewById(R.id.tv_title);
        toolbar.setNavigationIcon(R.mipmap.ic_back_left);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
        tvTitle.setText("本地记录");

        recycler.setLayoutManager(new LinearLayoutManager(this));
        recycler.setAdapter(adapter = new LocalHistoryAdapter(R.layout.item_local_history, list));
        recycler.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));
        adapter.setOnRecyclerViewItemChildClickListener(new BaseQuickAdapter.OnRecyclerViewItemChildClickListener() {
            @Override
            public void onItemChildClick(final BaseQuickAdapter adapter, View view, final int position) {
                final PatientLocalInfo data = (PatientLocalInfo) adapter.getData().get(position);
                switch (view.getId()) {
                    case R.id.ll_content:
                        Intent intent = new Intent(LocalHistoryActivity.this, HistoryDetailAct.class);
                        intent.putExtra("info", data);
                        startActivity(intent);
                        break;
                    case R.id.iv_delete:
                        AlertDialog alertDialog = DialogUtil.createAlertDialog(LocalHistoryActivity.this, "提醒", "是否删除本地记录？", "确定", "取消", true, new DialogUtil.DialogButtonClickListener() {
                            @Override
                            public void onPositiveButtonClick(DialogInterface dialog) {
                                dialog.dismiss();
                                MyInternalStorage myInternalStorage = new MyInternalStorage(LocalHistoryActivity.this);
                                //删除wav文件
                                boolean delete = myInternalStorage.delete(data.getFileName());
                                myInternalStorage.delete(data.getPcmFileName());

                                //删除数组
                                adapter.remove(position);
                                List<PatientLocalInfo> infoList = adapter.getData();

                                SPUtil.put(LocalHistoryActivity.this, Constant.LOCAL_INFO, GsonUtil.toJsonStr(infoList));
                                if (delete) {
                                    showToast("删除成功");
                                }
                            }

                            @Override
                            public void onNegativeButtonClick(DialogInterface dialog) {
                                dialog.dismiss();
                            }
                        });
                        alertDialog.show();
                        break;
                }
            }
        });
    }
}
